const db = wx.cloud.database()
Page({
  data: {
    deviceinfo: {}
  },
  onLoad: function(options) {
    const _id = options._id
    db.collection('device').doc(_id).get().then(res => {
      this.setData({
        deviceinfo: res.data
      })
    })
  },

  updateValue: function(event) {
    let name = event.currentTarget.dataset.name;
    let deviceinfo = this.data.deviceinfo
    deviceinfo[name] = event.detail.value
    this.setData({
      deviceinfo: deviceinfo
    })
  },

  deviceupdate: function(event) {
    let deviceinfo = this.data.deviceinfo
    db.collection('device').doc(deviceinfo._id).set({
      data: {
        deviceid: deviceinfo.deviceid,
        devicename: deviceinfo.devicename,
        deviceuser: deviceinfo.deviceuser,
        manufacturer: deviceinfo.manufacturer,
        place: deviceinfo.place,
        price: parseInt(deviceinfo.price),
        purchasedate: deviceinfo.purchasedate,
        supplier: deviceinfo.supplier
      },
      success: function(res) {
        if (res.stats.updated) {
          console.log("更新成功...")
        }
      }
    })
  },

  deviceDelete: function(event) {
    let deviceinfo = this.data.deviceinfo
    db.collection('device').doc(deviceinfo._id).remove()
      .then(res => {
        console.log("删除成功...")
        wx.navigateTo({
          url: '../home/home'
        })
      })
  },

})